package it.giuseppetrisciuoglio.lyrics.utils;

import java.text.Normalizer;
import java.util.UUID;
import java.util.regex.Pattern;

import org.apache.commons.lang3.StringUtils;

public class StringUtilies {

	private static final int MAX_CHAR_AMS_TRANSACTION = 16;

	public static String getStackTrace(Exception e) {
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < e.getStackTrace().length; i++) {
			StackTraceElement stackTraceElement = e.getStackTrace()[i];
			sb.append(stackTraceElement.toString());
		}
		return sb.toString();
	}

	public static String createTransactionCode() {
		return UUID.randomUUID().toString().replaceAll("-", "").substring(0, MAX_CHAR_AMS_TRANSACTION);
	}

	public static String replaceParamType(String type) {
		return type.replaceAll(" ", "-");
	}

	public static String replaceSpecialCharVocals(String value) {
		return StringUtils.stripAccents(value);
	}

	public static String removeSpecialCharVocals(String value) {
		String nfdNormalizedString = Normalizer.normalize(value, Normalizer.Form.NFD);
		Pattern pattern = Pattern.compile("\\p{InCombiningDiacriticalMarks}+");
		return pattern.matcher(nfdNormalizedString).replaceAll("");
	}

	public static String replaceSpecialChar(String value, String blankChar) {
		return value.replaceAll("[^a-zA-Z0-9]+", blankChar);
	}
	
	public static String replaceBrWithNewLine(String html){
		html = html.replaceAll("<br/>","\n");
		html = html.replaceAll("<br>","\n");
		return html.replaceAll("<br />","\n");
	}
	
	public static String stripHTMLtags(String html){
		return html.replaceAll("\\<.*?>","");
	}
	
	public static String removeLyricsCredits(String testo){
		if (testo.indexOf("(Grazie a") > 0 && testo.indexOf("per questo testo)") > 0){
			return testo.substring(0, testo.indexOf("(Grazie a"));
		}
		return testo;
	}
	
	public static String getTwitterURL(String testo){
		if (testo.indexOf("http://") > 0 && testo.indexOf("via @youtube") > 0){
			return testo.substring(testo.indexOf("http://"), testo.indexOf(" via @youtube"));
		}
		return testo;
	}
}
